package app.qrcode.browser.caculationtest

import android.app.Application
import android.content.Context
import android.content.SharedPreferences
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import app.qrcode.browser.caculationtest.MyApplication.Companion.context


/**
 * Created by DomenCai on 2022/2/11.
 */
private const val KEY_HIGH_SCORE = "key_high_score"
private const val KEY_LEFT_NUMBER = "key_left_number"
private const val KEY_RIGHT_NUMBER = "key_right_number"
private const val KEY_OPERATOR = "key_operator"
private const val KEY_ANSWER = "key_answer"
private const val SAVE_SHP_DATA_NAME = "save_shp_data_name"
private const val KEY_CURRENT_SCORE = "key_current_score"


class MyViewModel: ViewModel() {
    private var handle = SavedStateHandle()
    var winFlag: Boolean = false
    init {
        if (!handle.contains(KEY_HIGH_SCORE)) {
            val shp: SharedPreferences =
                context.getSharedPreferences(SAVE_SHP_DATA_NAME,Context.MODE_PRIVATE)
            handle.set(KEY_HIGH_SCORE, shp.getInt(KEY_HIGH_SCORE, 0))
            handle.set(KEY_LEFT_NUMBER, 0)
            handle.set(KEY_RIGHT_NUMBER, 0)
            handle.set(KEY_OPERATOR, "+")
            handle.set(KEY_ANSWER, 0)
            handle.set(KEY_CURRENT_SCORE, 0)
        }
    }

    fun getLeftNumber(): MutableLiveData<Int> {
        return handle.getLiveData(KEY_LEFT_NUMBER)
    }

    fun getRightNumber(): MutableLiveData<Int> {
        return handle.getLiveData(KEY_RIGHT_NUMBER)
    }

    fun getOperator(): MutableLiveData<String> {
        return handle.getLiveData(KEY_OPERATOR)
    }

    fun getHighScore(): MutableLiveData<Int> {
        return handle.getLiveData(KEY_HIGH_SCORE)
    }

    fun getCurrentScore(): MutableLiveData<Int> {
        return handle.getLiveData(KEY_CURRENT_SCORE)
    }

    fun getAnswer(): MutableLiveData<Int> {
        return handle.getLiveData(KEY_ANSWER)
    }

    fun generator() {
        val level = 20
        val x = (1..level).random()
        val y = (1..level).random()
        if (x % 2 == 0) {
            getOperator().value = "+"
            if (x > y) {
                getAnswer().value = x
                getLeftNumber().value = y
                getRightNumber().value = (x - y)
            } else {
                getAnswer().value = y
                getLeftNumber().value = x
                getRightNumber().value = (y - x)
            }
        } else {
            getOperator().value="-"
            if (x > y) {
                getAnswer().value = (x-y)
                getLeftNumber().value = x
                getRightNumber().value = y
            } else {
                getAnswer().value = (y-x)
                getLeftNumber().value = y
                getRightNumber().value = x
            }
        }
    }

    fun save() {
        val shp: SharedPreferences =
            context.getSharedPreferences(SAVE_SHP_DATA_NAME, Context.MODE_PRIVATE)
        val editor: SharedPreferences.Editor = shp.edit()
        editor.putInt(KEY_HIGH_SCORE, getHighScore().value!!)
        editor.apply()
    }

    fun answerCorrect() {
        getCurrentScore().value = getCurrentScore().value?.plus(1)
        if (getCurrentScore().value!! > getHighScore().value!!) {
            getHighScore().value=getCurrentScore().value
            winFlag=true
        }
        generator()
    }
}